<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 表头区域 -->
      <el-row :gutter="30">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
          >
            <el-button slot="append" icon="el-icon-search" @click="searchOrder"
              >搜索</el-button
            >
          </el-input></el-col
        >
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="ordersList" style="width: 100%" stripe border>
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column label="订单编号" prop="order_number">
        </el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="100">
        </el-table-column>
        <el-table-column label="是否付款" width="120">
          <template slot-scope="{ row }">
            <el-button
              v-if="row.order_pay === '0'"
              type="danger"
              size="mini"
              plain
              >未付款</el-button
            >
            <el-button v-else type="success" size="mini" plain
              >已付款</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="120">
        </el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="180">
          <template slot-scope="scope">
            {{ scope.row.create_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showEditAdressDialog"
          ></el-button>
          <el-button
            type="success"
            icon="el-icon-location"
            size="mini"
            @click="showProgressDialog"
          ></el-button>
        </el-table-column>
      </el-table>
      <!-- 页码区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 修改地址dialog -->
      <el-dialog
        title="修改地址"
        :visible.sync="editAdressVisible"
        width="45%"
        @close="adressFormClose"
      >
        <el-form
          :model="adressForm"
          :rules="adressFormRules"
          ref="adressFormRef"
          label-width="100px"
        >
          <el-form-item label="省市区县" prop="adress1">
            <el-cascader
              v-model="adressForm.adress1"
              :options="cityData"
              class="adressCascader"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="adress2">
            <el-input v-model="adressForm.adress2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editAdressVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAdressVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 物流详情dialog -->
      <el-dialog
        title="物流详情"
        :visible.sync="progressVisible"
        width="45%"
        @close="progressClose"
      >
        <el-timeline :reverse="false">
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.ftime"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="progressVisible = false">取 消</el-button>
          <el-button type="primary" @click="progressVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  name: 'Order',
  data() {
    return {
      ordersList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      editAdressVisible: false,
      adressForm: {
        adress1: '',
        adress2: '',
      },
      adressFormRules: {
        adress1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' },
          {
            min: 2,
            max: 20,
            message: '长度在 2 到 20 个字符',
            trigger: 'blur',
          },
        ],
        adress2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
          {
            min: 2,
            max: 30,
            message: '长度在 2 到 30 个字符',
            trigger: 'blur',
          },
        ],
      },
      cityData,
      progressVisible: false,
      progressInfo: [],
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    // 获取订单列表
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.ordersList = res.data.goods
      this.total = res.data.total
    },
    // 当商品页码显示条数改变时
    handleSizeChange(newPage) {
      this.queryInfo.pagesize = newPage
      this.getOrdersList()
    },
    // 当前页码发生改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrdersList()
    },
    // 查询订单
    searchOrder() {
      this.getOrdersList()
    },
    // 展示修改地址dialog
    showEditAdressDialog() {
      this.editAdressVisible = true
    },
    // 关闭编辑地址时重置表单
    adressFormClose() {
      this.$refs.adressFormRef.resetFields()
    },
    // 展示物流详情dialog
    async showProgressDialog() {
      this.progressVisible = true
      const { data: res } = await this.$http.get(`/kuaidi/1106975712662`)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.message)
      }
      this.progressInfo = res.data
    },
    // 关闭物流详情dialog
    progressClose() {
      this.progressInfo = []
    },
  },
  // 当搜索框数据发生改变时调用
  watch: {
    'queryInfo.query'(newValue, oldValue) {
      this.getOrdersList()
    },
  },
}
</script>

<style scoped>
.addRow {
  margin: 20px 0;
}
.addBtn {
  float: right;
}
.adressCascader {
  width: 100%;
}
</style>
